//
//  THBottomAlterView.m
//  YJB
//
//  Created by YJB on 2016/11/26.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THBottomAlterView.h"

HandlerBlock alterViewCheckBlock;
THBottomAlterView *alterView;
UIView *containView;
CGFloat const kanimateTime = 0.2f;
@implementation THBottomAlterView
+ (THBottomAlterView *)getBottomAlterView
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

+ (void)showWithCheckBlock:(HandlerBlock)checkBlock
{
    alterViewCheckBlock = checkBlock;
    THBottomAlterView *bottomAlterView = [self getBottomAlterView];
    alterView = bottomAlterView;
    bottomAlterView.layer.cornerRadius = 4;
    bottomAlterView.y = IPHONE_H;
    bottomAlterView.x = 2 * GRIDW;
    bottomAlterView.width = IPHONE_W - 2 * 2 * GRIDW;
    bottomAlterView.height = 156;
    UIWindow *win = [UIApplication sharedApplication].windows.lastObject;
    UIView *view = [[UIView alloc]initWithFrame:win.bounds];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissmis:)];
    [view addGestureRecognizer:tap];
    view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [win addSubview:view];
    [view addSubview:bottomAlterView];
    containView = view;
    
    [UIView animateWithDuration:kanimateTime animations:^{
        bottomAlterView.y = IPHONE_H - bottomAlterView.height - 25;
    }];
}

+ (void)dissmis:(UITapGestureRecognizer *)tap
{
    THBottomAlterView *bottomAlterView = containView.subviews.firstObject;
    [UIView animateWithDuration:kanimateTime animations:^{
        bottomAlterView.y = IPHONE_H;
    } completion:^(BOOL finished) {
       [containView removeFromSuperview];
        containView = nil;
        alterViewCheckBlock = nil;
        alterView = nil;
    }];
}

- (IBAction)checkClick:(UIButton *)button
{
    if (alterViewCheckBlock) {
        alterViewCheckBlock();
    }
    [UIView animateWithDuration:kanimateTime animations:^{
        alterView.y = IPHONE_H;
    } completion:^(BOOL finished) {
        [containView removeFromSuperview];
        containView = nil;
        alterViewCheckBlock = nil;

        alterView = nil;
    }];

}
- (IBAction)cancelClick:(UIButton *)button
{
    [UIView animateWithDuration:kanimateTime animations:^{
        alterView.y = IPHONE_H;
    } completion:^(BOOL finished) {
        [containView removeFromSuperview];
        containView = nil;
        alterViewCheckBlock = nil;
        alterView = nil;
    }];
}

@end
